//
//  CGameScene.m
//  ibasketball2D
//
//  Created by imac on 11-5-25.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CGameScene.h"

/*	主场景	*/

@implementation CGameScene

//主场景初始化,创建autorelease实例
+(id)scene
{
	CCScene *scene=[CCScene node];
	CGameScene* game=[CGameScene node];
	[scene addChild:game];
	return scene;
}

//主场景初始化
-(id)init
{
	if( (self=[super init]) )
	{
		m_GameState = FirstInit;
		m_ScenesTag = [[CScenesManager SharedManager] getSceneTag];

		[self scheduleUpdate];
	}
	return self;
}

//主场景更新函数
//ccTime: 时间间隔
-(void) update:(ccTime)delta
{
	GameState gamestate = [[CStateManager SharedManager] getGameState];
	//SceneTag scenetag =[[CScenesManager SharedManager] getSceneTag];
	
	if (m_GameState != gamestate) {
		switch (gamestate) {
			case Logo_Run:
				break;
				
			case MainMenu_Run:
				[self ShowMainMenu];
				break;
				
			case Game_Run:
				[self ShowGameLayer];
				break;
				
			case Option_Run:
				[self ShowOptionLayer];
				break;
				
			case HighScore_Run:
				[self ShowHighScore];
				break;
				
			case Credits_Run:
				[self ShowCredits];
				break;
				
			default:
				break;
		}
		m_GameState = gamestate;
	}
}

//显示主菜单画面
-(void)ShowMainMenu
{
	//清屏
	[self RemoveScenes];
	
	//获取场景管理器
	CScenesManager* scenemanager = [CScenesManager SharedManager];
	
	//从管理器处获取CMainMenu实例
	CMainMenu* temp = (CMainMenu*)[scenemanager GetSceneByTag:SceneTag_MainMenu];
	if (temp == nil) 
	{
		//若实例不存在则创建
		CMainMenu* mainmenu = [CMainMenu node];
		
		//添加到场景管理器
		[scenemanager AddGameScene:mainmenu withTag:SceneTag_MainMenu];
		
		//添加菜单到主场景
		[self addChild:mainmenu];
	}
	else 
	{
		//若存在则直接添加到主场景
		[self addChild:temp];
	}
}

//显示游戏层
-(void)ShowGameLayer
{
	//清屏
	[self RemoveScenes];
	
	CScenesManager* scenemanager = [CScenesManager SharedManager];
	CGameLayer* temp = [scenemanager GetSceneByTag:SceneTag_GameLayer];
	if (temp == nil) 
	{
		CGameLayer* gamelayer = [CGameLayer node];
		
		[scenemanager AddGameScene:gamelayer withTag:SceneTag_GameLayer];
		
		[self addChild:gamelayer];
	}
	else 
	{
		[self addChild:temp];
	}
}

//显示设置界面
-(void)ShowOptionLayer
{
	
}

//显示高分排名界面
-(void)ShowHighScore
{
	
}

//显示制作团队名单
-(void)ShowCredits
{
	
}

-(void)RemoveScenes
{
	[self removeAllChildrenWithCleanup:YES];
}

//内存释放
-(void) dealloc
{
	[self removeAllChildrenWithCleanup:YES];
	[[CCTextureCache sharedTextureCache] removeUnusedTextures];
	[super dealloc];
}

@end
